package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @package com.leyou.item.upload
 * @description: 查询商品分类的controller层
 * @author: 许超越
 * @date 2019/6/23 14:50
 * @version: V1.0
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据分类id显示分类信息
     * @param: id
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryByParentId(@RequestParam("pid") Long id) {
        //按照rest风格封装数据和状态码返回
        return ResponseEntity.ok(categoryService.queryByParentId(id));
    }


    /**
     * 通过品牌id查询商品类目
     * @param: bid
     * @return
     */
    @GetMapping("of/brand")
    public ResponseEntity<List<CategoryDTO>> queryByBrandId(@RequestParam("id") Long bid) {
        return ResponseEntity.ok(categoryService.queryByBrandId(bid));
    }

    /**
     * 根据品牌id删除品牌和类目对应的中间表
     * @param: bid
     * @return
     */
    @DeleteMapping("of/brand")
    public ResponseEntity<Void> deleteByBrandId(@RequestParam("id") Long bid) {
        categoryService.deleteByBrandId(bid);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id插叙分类
     * @param: idList 分类id集合
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> idList) {
        return ResponseEntity.ok(categoryService.queryCategoryByIds(idList));
    }


    /**
     * 根据3级分类id查询所有分类
     * @param: cid3 3级分类id
     * @return
     */
    @GetMapping("levels")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByCid3(@RequestParam("id") Long cid3) {
        return ResponseEntity.ok(categoryService.queryCategoryByCid3(cid3));
    }
}
